import pymysql  

#打开数据库连接  
db= pymysql.connect(host="localhost",user="root",  
    password="",db="zsmdb",port=3306,charset='utf8')  
  
# 使用cursor()方法获取操作游标  
cur = db.cursor()  
 
#1.查询操作  
# 编写sql 查询语句  user 对应我的表名  
sql = "select * from user"  
try:  
    cur.execute(sql)    #执行sql语句  
  
    results = cur.fetchall()    #获取查询的所有记录  
    print("学号","\t姓名","\t邮箱","\tmobile", "\tusersex")  
    #遍历结果  
    for row in results :  
        name = row[0] 
        psw = row[1]  
        email = row[2]

		
        print(name,psw,email,)  
except Exception as e:  
    raise e  
finally:  
    db.close()  #关闭连接  